package com.ss.android.usergrowth;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public final class ApkUtils {
    public static class ApkUtilsException extends Exception {
        private static final long serialVersionUID = -1169146753281314803L;

        public ApkUtilsException(Throwable arg1) {
            super(arg1);
        }

        public ApkUtilsException(String arg1) {
            super(arg1);
        }
    }

    String a;
    int b;
    AssetManager c;
    private Resources d;

    public ApkUtils(File arg5) throws ApkUtilsException {
        XmlResourceParser v0_3;
        int v1;

        String v0 = arg5.getAbsolutePath();
        try {
            Constructor constructor = Class.forName("android.content.res.AssetManager").getConstructor();
            constructor.setAccessible(true);
            this.c = (AssetManager) constructor.newInstance();
            Method method = AssetManager.class.getDeclaredMethod("addAssetPath", new Class[]{String.class});
            method.setAccessible(true);
            v1 = (int) method.invoke(this.c, new Object[]{v0});
            if (v1 == 0) {
                throw new ApkUtilsException("cannot add " + v0 + " to asset manager.");
            } else {
                try {
                    v0_3 = this.c.openXmlResourceParser(v1, "AndroidManifest.xml");
                    ApkUtils.a(((XmlPullParser) v0_3));
                    this.a = ApkUtils.a(v0_3, null, "package");
                    String v1_1 = ApkUtils.a(((XmlPullParser) v0_3), "http://schemas.android.com/apk/res/android", "versionCode");
                    if (!TextUtils.isEmpty(((CharSequence) v1_1)) && (TextUtils.isDigitsOnly(((CharSequence) v1_1)))) {
                        this.b = Integer.parseInt(v1_1);
                    }
                    ApkUtils.a(((XmlPullParser) v0_3), "http://schemas.android.com/apk/res/android", "versionName");
                    v0_3.close();
                } catch (IOException v0_2) {
                    throw new ApkUtilsException(((Throwable) v0_2));
                }
            }
        } catch (Throwable v0_1) {
            throw new ApkUtilsException(v0_1);
        }


    }

    private static void a(XmlPullParser arg2) throws ApkUtilsException {
        try {
            do {
                int v0_2 = arg2.next();
                if (v0_2 != 1) {
                    if (v0_2 != 2) {
                        continue;
                    }
                }
                break;
            } while (true);
        } catch (IOException v0) {
            throw new ApkUtilsException(((Throwable) v0));
        } catch (XmlPullParserException v0_1) {
            throw new ApkUtilsException(((Throwable) v0_1));
        }
        throw new ApkUtilsException("No start tag found");

    }

    private static String a(XmlPullParser arg2, String arg3, String arg4) throws ApkUtilsException {
        if (!"manifest".equals(arg2.getName())) {
            throw new ApkUtilsException("No <manifest> tag");
        }

        String v0 = arg2.getAttributeValue(arg3, arg4);
        return v0 == null || v0.length() == 0 ? "" : v0.intern();
    }

    public final Resources a() {
        if (this.d == null) {
            synchronized (this) {
                if (this.d == null) {
                    DisplayMetrics v0_1 = new DisplayMetrics();
                    v0_1.setToDefaults();
                    this.d = new Resources(this.c, v0_1, null);
                }
            }
        }

        return this.d;
    }
}

